var app = require('../../utils/util.js');
import {phone} from '../../utils/request/apiurl'
import api from '../../utils/request/api'

var app = require('../../utils/util.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
	processData: [{
	      name: '验证旧号码',
	      start: '../../asset/images/now01@2x.png',
	      end: '../../asset/images/yellow01@2x.png',
	      icon: '../../asset/images/yellow01@2x.png'
	    },
	    {
	      name: '绑定新号码',
	      start: '../../asset/images/now02@2x.png',
	      end: '../../asset/images/yellow02@2x.png',
	      icon: '../../asset/images/now02@2x.png'
	    },
	    {
	      name: '更换成功',
	      start: '../../asset/images/now03@2x.png',
	      end: '../../asset/images/yellow03@2x.png',
	      icon: '../../asset/images/gray03@2x.png'
	    }],
    phone:'',//手机号
    passWord:'',//验证码
    iscode:null,//用于存放验证码接口里获取到的code
    codename:'获取验证码',
	newPassWord:'',
	uid:'',
  numCode:60,//倒计时60s
  },
  //获取input输入框的值
  // 请输入手机号
  getPhoneValue:function(e){
	  // console.log(e)
    this.setData({
      phone:e.detail.value
    })
  },
  // 请输入验证码
  getCodeValue: function (e) {
    this.setData({
      code: e.detail.value
    })
  },
  getCode:function(){
    var a = this.data.phone;
    var _this = this;
    if (_this.data.numCode < 60 && _this.data.numCode > 0) {
      return wx.showToast({
        title: '请稍后发送验证码',
        icon: 'none'
      })
    }
    // var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
	var myreg=/^1(3|4|5|6|7|8|9)\d{9}$/;
    if (this.data.phone == "") {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return false;
    } else if (!myreg.test(this.data.phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return false;
    }else{
      var source={
      	uid:this.data.uid,
      	phone:this.data.phone,     
      }
      // console.log(source)
      api.post(phone.createCode,source).then(res => {
      	// console.log(res)
      	// this.setData({
      	// 	iscode:res,
      	// })
      	// var num = 61;
      	var timer = setInterval(function () {
          // num--;
          _this.setData({
            numCode: _this.data.numCode - 1,
          })
          if (_this.data.numCode <= 0) {
      	    clearInterval(timer);
      	    _this.setData({
      	      codename: '重新发送',
      	      disabled: false,
              numCode:60,
      	    })
      	 
      	  } else {
      	    _this.setData({
              codename: _this.data.numCode + "s"
      	    })
      	  }
      	}, 1000)
      	
      }).catch(err => {
      	// console.log(err)
      	wx.showToast({
      		title: err.message,
      		icon: 'none'
      	})
      })
      
    }  
  },
  //获取验证码
  getVerificationCode() {
    this.getCode();
    var _this = this
    _this.setData({
      disabled: true
    })
  },
  //提交表单信息
  save:function(){
	 var source={
	 	uid:this.data.uid,
	 	code:this.data.code,     
	 }
	 // console.log(source)
	 api.post(phone.verifyCode,source).then(res => {
	 	// console.log(res)
	 		if(res=='验证码正确'){
	 			var source={
	 				uid:this.data.uid,
	 				phone:this.data.phone,
	 				code:this.data.code,
	 			}
	 			// console.log(source)
	 			api.post(phone.addPhone,source).then(res => {
	 				// console.log(res)
	 				if(res==1){
						wx.redirectTo({
						  url: '../change-numdone/change-numdone',
						})//页面跳转
					}else{
						wx.showToast({
						    title: '添加失败',
						    icon: 'none',
						    success: function () {
						      setTimeout(function() {
						        wx.redirectTo({
						          url: '../change-phonenum/change-phonenum',
						        })//页面跳转
						      }, 1000);
						    }
						});
						
						// wx.showToast({
						// 	title: '添加失败',
						// 	icon: 'none'
						// })
						// wx.redirectTo({
						//   url: '../change-phonenum/change-phonenum',
						// })//页面跳转
					}	
	 			}).catch(err => {
	 				// console.log(err)
	 				wx.showToast({
	 					title: err.message,
	 					icon: 'none'
	 				})
	 			})	
	 		}	
	 }).catch(err => {
	 	// console.log(err)
	 	wx.showToast({
	 		title: err.message,
	 		icon: 'none'
	 	})
	 })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo.rank) {
    	this.setData({
    		uid: userInfo.uid,
    	})
    }
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
	// wx.showNavigationBarLoading() //在标题栏中显示加载
	// this.onLoad()
	// setTimeout(function() {
	// 	wx.hideNavigationBarLoading() //完成停止加载
	// 	wx.stopPullDownRefresh() //停止下拉刷新
	// }, 1500);
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //进度条的状态
    setPeocessIcon: function () {
      var index = 0//记录状态为1的最后的位置
      var processArr = this.data.processData
      // console.log("progress", this.data.detailData.progress)
      for (var i = 0; i < this.data.detailData.progress.length; i++) {
        var item = this.data.detailData.progress[i]
        processArr[i].name = item.word
        if (item.state == 1) {
          index = i
          processArr[i].icon = "../../img/process_3.png"
          processArr[i].start = "#45B2FE"
          processArr[i].end = "#45B2FE"
        } else {
          processArr[i].icon = "../../img/process_1.png"
          processArr[i].start = "#EFF3F6"
          processArr[i].end = "#EFF3F6"
        }
      }
      processArr[index].icon = "../../img/process_2.png"
      processArr[index].end = "#EFF3F6"
      processArr[0].start = "#fff"
      processArr[this.data.detailData.progress.length - 1].end = "#fff"
      this.setData({
        processData: processArr
      })
    },
})